﻿using KineBall.GameLogic;
using KineBall.Rendering;
using Microsoft.Xna.Framework;


namespace KineBall
{
    public static class ComponentManager
    {
        // Contains all the component we need to call when game start to execute
        public static void onStart(this KineBall game)
        {
            game.switchToMenu();
        }

        // Swtich to the Main Menu
        public static void switchToMenu(this KineBall game)
        {
            game.resetGameComponents();
            SoundManager.StopMusic();

            MenuComponent menu = new MenuComponent(game);
            menu.onePlayerGame = () => game.switchToGamePlay();
            
            // Future extensions
            // menu.twoPlayerGame = () => game.switchToGamePlay2(); 
            // menu.gameScore = () => game.switchToGameScore();

            game.Components.Add(menu);

            SoundManager.PlayMusic("Soundtrack01");
        }

        // Switch to one player match
        public static void switchToGamePlay(this KineBall game)
        {
            game.resetGameComponents();
            SoundManager.StopMusic();

            Renderer renderer = new Renderer(game);
            GameUpdater updater = new GameUpdater(game);
            updater.returnToMainMenu = () => game.switchToMenu();

            game.Components.Add(renderer);
            game.Components.Add(updater);

            SoundManager.PlayMusic("Soundtrack02");
        }


        /*** Game Components Utility ***/

        // Delete and Dispose all the components from the game.Components List
        public static void resetGameComponents(this KineBall game)
        {
            while (game.Components.Count > 0)
            {
                ((GameComponent)game.Components[0]).Dispose();
            }

            game.Components.Clear();

        }

        // Disable all the components in the game.Components List
        public static void disableAll(this KineBall game)
        {
            foreach (GameComponent component in game.Components)
            {
                component.Enabled = false;
            }
        }

        // Enable all the components in the game.Components List
        public static void enableAll(this KineBall game)
        {
            foreach (GameComponent component in game.Components)
            {
                component.Enabled = true;
            }
        }
    }
}
